# -*- coding: utf-8 -*-
"""
Created on Mon Nov 29 2021
@author: ShiFeng
"""
import os
import json
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.size'] = 9

current_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(current_path)

# dataset filenames
dataset_filename1 = "6U2U3mFAC_Equiphase_alpha_actual_net_6_6_None64_8-experts-SparseMoE_2023_12_20_1652.csv"
dataset_filename2 = "6U2U3mFAC_Equiphase_alpha_pred_net_12_8_None32_8-experts-SparseMoE_2023_12_20_1544.csv"
dataset_filename3 = "6U2U3mFAC_Equiphase_alpha_pred_net_12_8_None64_8-experts-SparseMoE_2023_12_21_1244cnnLSTM_512_3_01.csv"
dataset_filename4 = "6U2U3mFAC_Equiphase_alpha_pred_net_12_8_None64_8-experts-SparseMoE_2023_12_21_1244cnnLSTM_512_3_01cnnGru_512_3_01.csv"

print("loading data")
# each file has 128 rows
x_actual = pd.read_csv(dataset_filename1, header = None, usecols=[0])
y_pred = pd.read_csv(dataset_filename2, header = None, usecols=[0])
y_lstm = pd.read_csv(dataset_filename3, header = None, usecols=[0])
y_gru = pd.read_csv(dataset_filename4, header = None, usecols=[0])
# print(x_actual.shape, x_actual)
#
xx = np.linspace(47.2, 59.9, 128, endpoint=True)
# print(xx.shape, xx)
plt.figure(1, figsize=(4.25, 3.0))
## color ='red', color ='dimgray',
plt.plot(xx, y_lstm, color=(158/255, 72/255, 15/255), marker='^', linewidth=1.2,
         markersize=2, label='Predicted '+'E$_T$'+" by CNN-LSTM")
plt.plot(xx, y_gru, color=(112/255, 175/255, 70/255), marker='+', linewidth=1.2,
         markersize=2, label='Predicted '+'E$_T$'+" by CNN-GRU")

plt.plot(xx, x_actual, color ='red', marker='o', linewidth=1.2,
         markersize=2, label='Actual '+'E$_T$'+" on equiphase surface")
plt.plot(xx, y_pred, color ='dimgray', marker='s', linewidth=1.2,
         markersize=2, label='Predicted '+'E$_T$'+" by Transformer")
#  linewidth=1.2, label='Predictions by CNN-GRU'
plt.grid(True)

plt.xlim((47.0, 60.1))
plt.ylim((55, 137))
plt.xlabel('Frequency(GHz)')
plt.ylabel('Electric field value (V/m)')
# plt.ylabel('Angle α(°)')
# plt.legend(loc='upper left', framealpha = False)
plt.legend(loc='upper left', ncol=2, frameon = False, fontsize = 6.5)
# plt.grid(axis='y')
plt.grid(True)
plt.tight_layout()
plt.subplots_adjust(left=0.13)
plt.subplots_adjust(bottom=0.15)
# save figs
# plt.savefig("Equiphase_alpha_"+Postfix, dpi=300)
plt.savefig(dataset_filename1[:-4] + "_paper_fig.png", dpi=300, bbox_inches="tight")
# plt.savefig(DATASET_FILE_NAME[:-4] + "_paper_fig.pdf", dpi=300, bbox_inches="tight")
print("saving prediction figs.")
plt.close('all')

##################################################################################
####                        draw bias   
##################################################################################
plt.figure(2, figsize=(4.25, 3.0))
## color ='red', color ='dimgray',
plt.plot(xx, x_actual - y_lstm, color=(158/255, 72/255, 15/255), marker='^', linewidth=1.2,
         markersize=2, label= "Bias by CNN-LSTM")
plt.plot(xx, x_actual - y_gru, color=(112/255, 175/255, 70/255), marker='+', linewidth=1.2,
         markersize=2, label= "Bias by CNN-GRU")
plt.plot(xx, x_actual -y_pred, color ='red', marker='s', linewidth=1.2,
         markersize=2, label= "Bias by Transformer")
#  linewidth=1.2, label='Predictions by CNN-GRU'
plt.grid(True)


plt.xlim((47.0, 60.1))
plt.ylim((-5, 5))
plt.tight_layout()
plt.xlabel('Frequency(GHz)')
plt.ylabel('Bias (V/m)')
plt.legend(loc='upper left', ncol=2, frameon = False, fontsize = 6.5)
plt.subplots_adjust(left=0.15)
plt.subplots_adjust(bottom=0.15)

plt.savefig(dataset_filename1[:-4]+'_bias', dpi=300)
# plt.savefig("Figure 9-b.pdf", dpi=300)
print("saving bias figures.")
# plt.show()
plt.close('all')

